package com.yanqu.road.server.manger.activity.harps;

import com.yanqu.road.entity.activity.ActivityConfig;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.harps.HarpsConfigSync;
import com.yanqu.road.entity.activity.harps.HarpsMountConfig;
import com.yanqu.road.entity.activity.suitang.config.*;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.enums.eSkillType;
import com.yanqu.road.entity.player.UserHistoryAttribute;
import com.yanqu.road.entity.union.activitygroup.UnionActivityGroup;
import com.yanqu.road.logic.activity.suitang.SuiTangMapLogic;
import com.yanqu.road.logic.bussiness.activity.HarpsBusiness;
import com.yanqu.road.logic.bussiness.activity.SuiTangActivityBusiness;
import com.yanqu.road.logic.bussiness.player.UserHistoryAttributeBussiness;
import com.yanqu.road.logic.helper.UnionActivityGroupHelper;
import com.yanqu.road.pb.activity.suitang.SuiTang;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.harps.HarpsModule;
import com.yanqu.road.server.gameplayer.module.activity.suitang.SuiTangModule;
import com.yanqu.road.server.gameplayer.module.patrons.PatronsModule;
import com.yanqu.road.server.gameplayer.module.player.PlayerModule;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.activity.curio.CurioMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.manger.union.GameUnionActivityGroupMgr;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.utils.RandomHelper;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

public class HarpsMgr extends TempMgr {

    private static HarpsConfigSync config = null;

    private static RandomHelper randomHelper = new RandomHelper();

    public static RandomHelper getRandomHelper() {
        return randomHelper;
    }

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    /**
     * 重新加载活动
     */
    public static void reloadActivityData() {
        getLogger().info("reload HarpsActivity start");
        List<ActivityInfo> list = NormalActivityMgr.getOpenActivityInfoList(eActivityType.Harps.getValue());
        if (list.isEmpty()) {
            config = null;
            getLogger().info("reload HarpsActivity end, no activity in show time.");
            return;
        }

        loadConfigFromDb(list.get(0));

        getLogger().info("reload HarpsActivity end, activityId {}.", config.getActivityInfo().getActivityId());

        for (GamePlayer player : GamePlayerMgr.getAllOnlinePlayer()) {
            player.getModule(HarpsModule.class).activityLogin();
        }
    }

    /**
     * 数据库加载配置
     */
    private static void loadConfigFromDb(ActivityInfo activityInfo) {
        if (activityInfo == null) {
            return;
        }
        int activityId = activityInfo.getActivityId();
        Map<String, ActivityConfig> activityConfigMap = NormalActivityMgr.getActivityConfigMap(activityInfo.getActivityId());

        HarpsConfigSync tmpConfig = new HarpsConfigSync();
        tmpConfig.setActivityId(activityId);
        tmpConfig.setActivityInfo(activityInfo);
        tmpConfig.setMountConfigMap(HarpsBusiness.getGenericConfigMap(activityId, HarpsMountConfig.class));
        tmpConfig.setActivityConfigMap(activityConfigMap);
        tmpConfig.getParamConfig().initParamConfig(activityConfigMap);
        tmpConfig.initHighestReward();
        config = tmpConfig;
    }

    /**
     * 拿配置
     */
    public static HarpsConfigSync getConfig() {
        return config;
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    @Override
    public boolean stop() {
        return true;
    }

    @Override
    public boolean save() {
        return super.save();
    }
}
